package com.mac.service;

import com.mac.mapper.MessagesMapper;
import com.mac.mapper.UserMapper;
import com.mac.pojo.Messages;
import com.mac.pojo.PageBean;
import com.mac.pojo.User;
import com.mac.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;

@SuppressWarnings("all")
public class MessagesService{
    private static final Logger LOGGER = LoggerFactory.getLogger("MessagesService.Class");

    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
    public boolean insertMessage(Messages message) {
        SqlSession session = factory.openSession();
        MessagesMapper mapper = session.getMapper(MessagesMapper.class);
        try {
            mapper.insertMessage(message);
            session.commit();
            LOGGER.info("insertMessage(massage) -> Succeed");
            return true;
        } catch (Exception e) {
            session.close();
            LOGGER.error("insertMessage(massage) -> failed", e);
            return false;
        }
    }

    public Messages selectMessageById(int messageId) {
        SqlSession session = factory.openSession();
        MessagesMapper mapper = session.getMapper(MessagesMapper.class);
        try {
            Messages m = mapper.selectMessageById(messageId);
            session.close();
            LOGGER.info("selectMessageById(messageId) ->succeed");
            return m;
        } catch (Exception e) {
            session.close();
            LOGGER.error("selectMessageById(messageId) ->failed", e);
            return null;
        }
    }

    public ArrayList<Messages> selectMessageByUserId(int userId) {
        SqlSession session = factory.openSession();
        MessagesMapper mapper = session.getMapper(MessagesMapper.class);
        ArrayList<Messages> messagesList;
        try {
            messagesList = mapper.selectMessageByUserId(userId);
            session.commit();
            LOGGER.info("selectMessageByUserId(userId) -> succeed", messagesList);
            return messagesList;
        } catch (Exception e) {
            session.close();
            LOGGER.error("selectMessageByUserId(userId) -> failed", e);
            return null;
        }

    }

    public ArrayList<Messages> selectAllMessages() {
        SqlSession session = factory.openSession();
        MessagesMapper mapper = session.getMapper(MessagesMapper.class);
        ArrayList<Messages> messagesList;
        try {
            messagesList = mapper.selectAllMessages();
            session.commit();
            LOGGER.info("selectAllMessages() -> succeed",messagesList);
            return messagesList;
        } catch (Exception e) {
            session.close();
            LOGGER.error("selectAllMessages() -> failed", e);
            return null;

        }
    }

    public boolean updateMessage(Messages message) {
       SqlSession session = factory.openSession();
       MessagesMapper mapper = session.getMapper(MessagesMapper.class);
       Messages m = mapper.selectMessageById(message.getMessageId());
       if(m != null){
           try {
               mapper.updateMessage(message);
               session.commit();
               LOGGER.info("updateMessage(message) -> succeed");
               return true;
           } catch (Exception e) {
               session.close();
               LOGGER.error("updateMessage(message) ->  failed", e);
           }
       }
       return m==null;
    }

    public boolean deleteMessage(int messageId) {
        SqlSession session = factory.openSession();
        MessagesMapper mapper = session.getMapper(MessagesMapper.class);
        Messages m = mapper.selectMessageById(messageId);
        if(m != null){
            try {
                mapper.deleteMessage(messageId);
                session.commit();
                LOGGER.info("deleteMessage(int messageId) -> succeed");
                return true;
            } catch (Exception e) {
                session.close();
                LOGGER.error("deleteMessage(int messageId) -> failed", e);
                return false;
            }

        }
        return false;


    }

    public ArrayList<User> selectAll() {
        SqlSession sqlSession = factory.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        ArrayList<User> users;
        try {
            users = userMapper.selectAll();
            LOGGER.info("userMapper.selectAll() -> succeed");
            sqlSession.close();
            return users;
        } catch (Exception e) {
            LOGGER.error("userMapper.selectAll() -> failed",e);
            sqlSession.close();
            throw new RuntimeException(e);
        }
    }

    public boolean deleteByIds(int[] ids) {
        SqlSession sqlSession = factory.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        try {
            userMapper.deleteByIds(ids);
            sqlSession.commit();
            sqlSession.close();
            LOGGER.info("userMapper.deleteByIds(ids) -> succeed");
            return true;
        } catch (Exception e) {
            LOGGER.error("userMapper.deleteByIds(ids) -> failed",e);
            sqlSession.close();
            throw new RuntimeException(e);
        }
    }

    public PageBean<User> selectByPage(int currentPage, int pageSize) {

        //4. 计算开始索引
        int begin = (currentPage - 1) * pageSize;
        // 计算查询条目数
        int size = pageSize;

        SqlSession sqlSession = factory.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        ArrayList<User> users;
        try {
            users = userMapper.selectByPage(begin,size);

            //6. 查询总记录数
            int totalCount = userMapper.selectTotalCount();

            //7. 封装PageBean对象
            PageBean<User> pageBean = new PageBean<>();
            pageBean.setRows(users);
            pageBean.setTotalCount(totalCount);

            LOGGER.info("userMapper.selectByPage(begin,size) -> succeed");
            sqlSession.close();
            return pageBean;
        } catch (Exception e) {
            LOGGER.error("userMapper.selectByPage(begin,size) -> failed",e);
            sqlSession.close();
            throw new RuntimeException(e);
        }
    }

    public Integer selectTotalCount() {
        SqlSession sqlSession = factory.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        Integer total;
        try {
            total = userMapper.selectTotalCount();
            LOGGER.info("userMapper.selectTotalCount() -> succeed");
            sqlSession.close();
            return total;
        } catch (Exception e) {
            LOGGER.error("userMapper.selectTotalCount() -> failed",e);
            sqlSession.close();
            throw new RuntimeException(e);
        }
    }

    public PageBean<User> selectByPageAndCondition(int currentPage, int pageSize, User user) {

        int begin = (currentPage - 1) * pageSize;
        // 计算查询条目数
        int size = pageSize;

        SqlSession sqlSession = factory.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        ArrayList<User> users;
        try {
            users = userMapper.selectByPageAndCondition(begin,size,user);


            //6. 查询总记录数
            int totalCount = userMapper.selectTotalCountByCondition(user);

            //7. 封装PageBean对象
            PageBean<User> pageBean = new PageBean<>();
            pageBean.setRows(users);
            pageBean.setTotalCount(totalCount);

            LOGGER.info("selectByPageAndCondition(begin,size,user) -> succeed");
            sqlSession.close();
            return pageBean;
        } catch (Exception e) {
            LOGGER.error("selectByPageAndCondition(begin,size,user) -> failed",e);
            sqlSession.close();
            throw new RuntimeException(e);
        }
    }

    public Integer selectTotalCountByCondition(User user) {
        SqlSession sqlSession = factory.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        Integer total;
        try {
            total = userMapper.selectTotalCountByCondition(user);
            LOGGER.info("userMapper.selectTotalCountByCondition(user) -> succeed");
            sqlSession.close();
            return total;
        } catch (Exception e) {
            LOGGER.error("userMapper.selectTotalCountByCondition(user) -> failed",e);
            sqlSession.close();
            throw new RuntimeException(e);
        }
    }
}
